package com.speedata;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.speedata.uhftest.R;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Objects;

/**
 * @author xuyan  SP60测试
 */
public class Sp60TestActivity extends AppCompatActivity implements ToggleButton.OnCheckedChangeListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sp60_test);
        initView();
        //intentFilter();
    }

    private void intentFilter() {
        IntentFilter filter = new IntentFilter();


        //定制广播
        filter.addAction("com.zlsoft.mobile.bar");

        registerReceiver(receiver, filter);
    }


    /**
     * 广播接收器
     */
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (TextUtils.isEmpty(intent.getAction())) {
                return;
            }
            switch (Objects.requireNonNull(intent.getAction())) {
                case "com.zlsoft.mobile.bar":
                    String msg = intent.getStringExtra("ZL_BAR_VALUE");
                    Toast.makeText(Sp60TestActivity.this, "扫描结果：" + msg, Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }

        }
    };

    private void initView() {
        ToggleButton bt1 = findViewById(R.id.bt1);

        bt1.setOnCheckedChangeListener(this);
        String qian = "313233";
        String qian2 = hexToASCII(qian);
        //String hou2 = Arrays.toString(hou.getBytes());

        bt1.setText(qian2);
    }

    public static String hexToASCII(String hex) {
        // initialize the ASCII code string as empty.
        String ascii = "";

        for (int i = 0; i < hex.length(); i += 2) {

            // extract two characters from hex string
            String part = hex.substring(i, i + 2);

            // change it into base 16 and typecast as the character
            char ch = (char) Integer.parseInt(part, 16);

            // add this char to final ASCII string
            ascii = ascii + ch;
        }

        return ascii;
    }


    @Override
    protected void onDestroy() {

        //openCloseTorch("off");
        unregisterReceiver(receiver);
        super.onDestroy();
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        //补光灯
        if (buttonView.getId() == R.id.bt1) {
            if (isChecked) {
//                openCloseTorch("en");
//                openCloseTorch("on");
                Intent intent = new Intent("com.spd.action.KEY_SCAN");
                intent.putExtra("keyAction", KeyEvent.ACTION_DOWN);
                sendBroadcast(intent);
            } else {
//                openCloseTorch("off");
                Intent intent = new Intent("com.spd.action.KEY_SCAN");
                intent.putExtra("keyAction", KeyEvent.ACTION_UP);
                sendBroadcast(intent);
            }
        }
    }

    /**
     * 补光灯
     *
     * @param str onoff
     */
    public static void openCloseTorch(String str) {

        File torchFileName = new File("/sys/class/misc/lm3642/torch");
        try {
            BufferedWriter torchFileWrite = new BufferedWriter(new FileWriter(torchFileName, false));
            torchFileWrite.write(str);
            torchFileWrite.flush();
            torchFileWrite.close();
        } catch (IOException e) {
            e.printStackTrace();

        }

    }


}